﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;
using PowerCreator.LMS.Web.Question;
using PowerCreator.LMS.Question.Entiry;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;

public partial class sysadmin_QuestionManager_CombinationQuestion_Edit_danx : PowerCreator.LMS.Web.Base.BasePage
{
    ManagerQuestion objQuestion = new ManagerQuestion();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string QuestionID = Base64.Decode(Request.QueryString["qid"].ToString());
            string FatherQuestionID = Base64.Decode(Request.QueryString["fthID"].ToString());
            ViewState["QuestionID"] = QuestionID;
            ViewState["FatherQuestionID"] = FatherQuestionID;
            //ViewState["UserID"] = GetCreatedByInfo().UserID;
            ViewState["TypeID"] = 2;
            
            Pageload();
            InitPage();
        }
    }
    protected override void InitPage()
    {
        lab_TitleInfo.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_ContentTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_QuestionCount.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionContent", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_QuestionOption.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionOption", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_RightAnswer.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_RightAnswer", PowerCreator.LMS.Language.LanagetItemType.Page);

        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);


        btn_AddOptionC.Text = GetLanageValueByKey("btn_AddQuestionInfo_danx_AddOption", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_AddOptionD.Text = GetLanageValueByKey("btn_AddQuestionInfo_danx_AddOption", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_AddOptionE.Text = GetLanageValueByKey("btn_AddQuestionInfo_danx_AddOption", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    private void Pageload()
    {
        VOQuestion voQuestion = new VOQuestion();
        voQuestion = objQuestion.LoadQuestion(int.Parse(ViewState["QuestionID"].ToString()));
        ViewState["objQuestionInfo"] = voQuestion;

        DataTable dt_QuestionOption = new DataTable();
        string search = "FKQuestionOptionQuestionID=" + ViewState["QuestionID"].ToString();
        dt_QuestionOption = objQuestion.QueryQuestionOption(search);
        ViewState["dt_QuestionOption"] = dt_QuestionOption;

        txtQuestionContent.Value = voQuestion.QuestionContent;


        #region 绑定选项
        ListItem objlist;
        for (int i = 0; i < dt_QuestionOption.Rows.Count; i++)
        {
            objlist = new ListItem();
            switch (i)
            {
                case 0: objlist.Text = "A";
                    //objlist.Selected = true;
                    break;
                case 1: objlist.Text = "B";
                    break;
                case 2: objlist.Text = "C";
                    break;
                case 3: objlist.Text = "D";
                    break;
                case 4: objlist.Text = "E";
                    break;
                case 5: objlist.Text = "F";
                    break;
            }
            objlist.Value = i.ToString();

            rdbtn_answer.Items.Add(objlist);
        }
        if (voQuestion.QuestionAnswer == "A")
        {
            rdbtn_answer.Items[0].Selected = true;
        }
        else if (voQuestion.QuestionAnswer == "B")
        {
            rdbtn_answer.Items[1].Selected = true;
        }
        else if (voQuestion.QuestionAnswer == "C")
        {
            rdbtn_answer.Items[2].Selected = true;
        }
        else if (voQuestion.QuestionAnswer == "D")
        {
            rdbtn_answer.Items[3].Selected = true;
        }
        else if (voQuestion.QuestionAnswer == "E")
        {
            rdbtn_answer.Items[4].Selected = true;
        }
        else
        {
            rdbtn_answer.Items[5].Selected = true;
        }
        #endregion

        #region 绑定选项内容



        for (int i = 0; i < dt_QuestionOption.Rows.Count; i++)
        {
            string Content = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString();
            string Option = Content.Substring(0, 1);
            if (Option == "A")
            {
                txtOptionA.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }
            else if (Option == "B")
            {
                txtOptionB.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }
            else if (Option == "C")
            {
                txtOptionC.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }
            else if (Option == "D")
            {
                txtOptionD.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }
            else if (Option == "E")
            {
                txtOptionE.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }
            else
            {
                txtOptionF.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }

        }
        #endregion

        #region 隐藏未选的选项
        if (dt_QuestionOption.Rows.Count == 1)
        {
            btn_AddOptionA.Visible = true;
            Pnl_B.Visible = false;
            Pnl_C.Visible = false;
            Pnl_D.Visible = false;

            Pnl_E.Visible = false;
            Pnl_F.Visible = false;
        }
        if (dt_QuestionOption.Rows.Count == 2)
        {
            btn_AddOptionA.Visible = false;
            Pnl_B.Visible = true;

            Pnl_C.Visible = false;
            Pnl_D.Visible = false;

            Pnl_E.Visible = false;
            Pnl_F.Visible = false;
        }
        if (dt_QuestionOption.Rows.Count == 3)
        {
            btn_AddOptionA.Visible = false;

            Pnl_B.Visible = true;
            btn_AddOptionB.Visible = false;

            Pnl_C.Visible = true;
            Pnl_D.Visible = false;

            Pnl_E.Visible = false;
            Pnl_F.Visible = false;
        }
        if (dt_QuestionOption.Rows.Count == 4)
        {
            btn_AddOptionA.Visible = false;

            Pnl_B.Visible = true;
            btn_AddOptionB.Visible = false;

            Pnl_C.Visible = true;
            btn_AddOptionC.Visible = false;

            Pnl_D.Visible = true;

            Pnl_E.Visible = false;
            Pnl_F.Visible = false;
        }
        if (dt_QuestionOption.Rows.Count == 5)
        {
            btn_AddOptionA.Visible = false;

            Pnl_B.Visible = true;
            btn_AddOptionB.Visible = false;

            Pnl_C.Visible = true;
            btn_AddOptionC.Visible = false;

            Pnl_D.Visible = true;
            btn_AddOptionD.Visible = false;

            Pnl_E.Visible = true;
            Pnl_F.Visible = false;
        }
        if (dt_QuestionOption.Rows.Count == 6)
        {
            btn_AddOptionA.Visible = false;

            Pnl_B.Visible = true;
            btn_AddOptionB.Visible = false;

            Pnl_C.Visible = true;
            btn_AddOptionC.Visible = false;

            Pnl_D.Visible = true;
            btn_AddOptionD.Visible = false;

            Pnl_E.Visible = true;
            btn_AddOptionE.Visible = false;
            Pnl_F.Visible = true;
        }
        #endregion
    }

    private string ConvertOptionID(int Index)
    {
        string OptionID = string.Empty;
        switch (Index)
        {
            case 0: OptionID = "A";
                break;
            case 1: OptionID = "B";
                break;
            case 2: OptionID = "C";
                break;
            case 3: OptionID = "D";
                break;
            case 4: OptionID = "E";
                break;
            case 5: OptionID = "F";
                break;
        }
        return OptionID;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        ArrayList list = new ArrayList();

        ArrayList Optionlist = new ArrayList();
        Optionlist.Add(txtOptionA.Text.Trim());
        Optionlist.Add(txtOptionB.Text.Trim());
        Optionlist.Add(txtOptionC.Text.Trim());
        Optionlist.Add(txtOptionD.Text.Trim());
        Optionlist.Add(txtOptionE.Text.Trim());
        Optionlist.Add(txtOptionF.Text.Trim());


        for (int i = 0; i < rdbtn_answer.SelectedIndex + 1; i++)
        {
            string OptionID = ConvertOptionID(i);

            if (Optionlist[i].ToString() == "")
            {
                Script.Alert(Page, "msg", OptionID + GetLanageValueByKey("msg_AddQuestionInfo_checkmsg2", PowerCreator.LMS.Language.LanagetItemType.Page));
                return;
            }
        }
        for (int i = 0; i < Optionlist.Count; i++)
        {
            string OptionID = ConvertOptionID(i);
            if (Optionlist[i].ToString() != "")
            {
                list.Add(OptionID + "、" + Optionlist[i].ToString());
            }
        }

        #region 删除选项

        DataTable dt_QuestionOption = (DataTable)ViewState["dt_QuestionOption"];
        for (int i = 0; i < dt_QuestionOption.Rows.Count; i++)
        {
            string Content = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString();
            string Option = Content.Substring(0, 1);
            string optionID = dt_QuestionOption.Rows[i]["QuestionOptionID"].ToString();
            objQuestion.DeleteQuestionOption(int.Parse(optionID));

        }
        #endregion

        VOQuestion voQuestion = new VOQuestion();
        voQuestion = (VOQuestion)ViewState["objQuestionInfo"];
        //voQuestion.FKQuestionCourseID = int.Parse(ViewState["FKQuestionCourseID"].ToString());
        //voQuestion.QuestionCreateID = int.Parse(Session["UserID"].ToString());
        ////info.DateCreated = DateTime.Now;

        voQuestion.QuestionAnswer = rdbtn_answer.SelectedItem.Text;
        //voQuestion.QuestionDefaultScore = int.Parse(ViewState["QuestionDefaultScore"].ToString());
        //voQuestion.QuestionDiffculty = int.Parse(ViewState["QuestionDiffculty"].ToString());
        //voQuestion.QuestionKen = ViewState["QuestionDiffculty"].ToString();


        voQuestion.QuestionContent = txtQuestionContent.Value;


        //voQuestion.QuestionState = int.Parse(ViewState["QuestionState"].ToString());  //禁用1,启用2

        VOQuestionOption objQuestionOptionInfo = new VOQuestionOption();

        for (int i = 0; i < list.Count; i++)
        {
            objQuestionOptionInfo.FKQuestionOptionQuestionID = voQuestion.QuestionID;
            objQuestionOptionInfo.QuestionOptionContent = list[i].ToString();
            objQuestionOptionInfo.QuestionOptionCreateID = voQuestion.QuestionCreateID;
            objQuestionOptionInfo.QuestionOptionCreateTime = DateTime.Now;
            objQuestion.CreateQuestionOption(objQuestionOptionInfo);
        }


        if (objQuestion.UpdateQuetion(voQuestion))//更新试题信息
        {
            Script.ShowAndHistory(GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page), -2);
        }
        else
        {
            Script.Alert(GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
        }

    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Script.RedirectURL("EditQuestionInfo_zuhe.aspx?qid=" + ViewState["FatherQuestionID"].ToString());
    }
    protected void btn_AddOptionA_Click(object sender, EventArgs e)
    {
        ListItem objlist = new ListItem();
        objlist.Text = "B";
        objlist.Value = "1";
        rdbtn_answer.Items.Add(objlist);
        Pnl_B.Visible = true;
        btn_AddOptionA.Visible = false;
    }

    protected void btn_AddOptionB_Click(object sender, EventArgs e)
    {
        ListItem objlist = new ListItem();
        objlist.Text = "C";
        objlist.Value = "2";
        rdbtn_answer.Items.Add(objlist);
        Pnl_C.Visible = true;
        btn_AddOptionB.Visible = false;
    }
    protected void btn_AddOptionC_Click(object sender, EventArgs e)
    {
        ListItem objlist = new ListItem();
        objlist.Text = "D";
        objlist.Value = "3";
        rdbtn_answer.Items.Add(objlist);
        Pnl_D.Visible = true;
        btn_AddOptionC.Visible = false;
    }
    protected void btn_AddOptionD_Click(object sender, EventArgs e)
    {
        ListItem objlist = new ListItem();
        objlist.Text = "E";
        objlist.Value = "4";
        rdbtn_answer.Items.Add(objlist);
        Pnl_E.Visible = true;
        btn_AddOptionD.Visible = false;
    }
    protected void btn_AddOptionE_Click(object sender, EventArgs e)
    {
        ListItem objlist = new ListItem();
        objlist.Text = "F";
        objlist.Value = "5";
        rdbtn_answer.Items.Add(objlist);
        Pnl_F.Visible = true;
        btn_AddOptionE.Visible = false;
    }
}
